package generator;
// Based on project found at: https://github.com/Clairette/sudoku/

public class Board
{
	private Cell[][] board = new Cell[9][9];
	
	public Board() { }
	
	public Cell getCell(int col, int row)
	{
		return board[col][row];
	}
	
	public void setCell(int col, int row, Cell c)
	{
		board[col][row] = c;
	}
	
	public void setCell(int col, int row, int number, boolean fixed)
	{
		if (board[col][row] == null)
		{
			board[col][row] = new Cell(number, fixed);
		}
		else
		{
			board[col][row].setNumber(number);
			board[col][row].setFixed(fixed);
		}
	}
	
	public Cell[] getCol(int row)
	{
		Cell[] col = new Cell[9];
		
		for (int i = 0; i < 9; i++)
		{
			col[i] = board[i][row];
		}
		return col;
	}
	
	public Cell[] getRow(int col)
	{
		return board[col];
	}
	
	public Cell[] getBlock(int col, int row)
	{
		// could use improvement
		Cell[] block = new Cell[9];
		
		col = col - (col % 3);
		row = row - (row % 3);
		int b = 0;
		
		for (int n = col; n < col + 3; n++)
		{
			for (int m = row; m < row + 3; m++)
			{
				block[b] = board[n][m];
				b++;
			}
		}
		return block;
	}
	
	public boolean contains(int colNum, int rowNum, int number)
	{
		Cell[] col = getCol(rowNum);
		Cell[] row = getRow(colNum);
		Cell[] block = getBlock(colNum, rowNum);
		
		if (contains(col, number) || contains(row, number) || contains(block, number))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private boolean contains(Cell[] array, int n)
	{
		for (int i = 0; i < array.length; i++)
		{
			if (array[i] != null && array[i].equals(n))
			{
				return true;
			}
		}
		return false;
	}
	
	public void printBoard()
	{
		System.out.println("-------------------");
		for (int col = 0; col < 9; col++)
		{
			System.out.print("|");
			for (int row = 0; row < 9; row++)
			{
				if (board[col][row] == null || board[col][row].equals(0))
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(board[col][row]);
				}
				if (row % 3 == 2)
				{
					System.out.print("|");
				}
				else
				{
					System.out.print(" ");
				}
			}
			if (col % 3 == 2)
			{
				System.out.println("\n-------------------");
			}
			else
			{
				System.out.println();
			}
		}
	}
	
	public void copy(Board board)
	{
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				this.board[col][row] = board.getCell(col, row);
			}
		}
	}
	
	public void clear()
	{
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				this.board[col][row] = new Cell(0, false);
			}
		}
	}
	
	public void reset()
	{
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				if (!getCell(col, row).isFixed())
				{
					this.board[col][row] = new Cell(0, false);
				}
			}
		}
	}
	
	public boolean[][] compare(Board compBoard)
	{
		boolean[][] bools = new boolean[9][9];
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				if (getCell(col, row).equals(compBoard.getCell(col, row)))
				{
					bools[col][row] = true;
				}
				else
				{
					bools[col][row] = false;
				}
			}
		}
		return bools;
	}
	
	public boolean equals(Board board)
	{
		boolean[][] matchMatrix = this.compare(board);
		
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				if (!matchMatrix[col][row])
				{
					return false;
				}
			}
		}
		return true;
	}
	
	public String getPuzzle()
	{
		String puzzle = "";
		
		for (int col = 0; col < 9; col++)
		{
			for (int row = 0; row < 9; row++)
			{
				if (board[col][row] == null || board[col][row].equals(0))
				{
					puzzle += 0;
				}
				else
				{
					puzzle += board[col][row];
				}
			}
		}
		
		return puzzle;
	}
}